package com.gmu.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gmu.pojo.TblStudent;
import com.gmu.pojo.TblTeacher;
import com.gmu.pojo.TblUnits;
import com.gmu.pojo.TblUser;
import com.gmu.pojo.excel.TeacherUserData;
import com.gmu.service.TblTeacherService;
import com.gmu.service.TblUnitsService;
import com.gmu.service.TblUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.security.crypto.password.PasswordEncoder;

public class TeacherExcelListener extends AnalysisEventListener<TeacherUserData> {

    //因为UserExcelListener不能交给spring管理，所以需要手动注入
    public TblUserService tblUserService;

    public TblTeacherService tblTeacherService;

    public PasswordEncoder passwordEncoder;

    public TblUnitsService unitsService;


    public TeacherExcelListener(TblUserService tblUserService, TblTeacherService tblTeacherService,TblUnitsService unitsService,PasswordEncoder passwordEncoder) {
        this.tblUserService = tblUserService;
        this.tblTeacherService = tblTeacherService;
        this.passwordEncoder = passwordEncoder;
        this.unitsService = unitsService;
    }

    @Override
    public void invoke(TeacherUserData teacherUserData, AnalysisContext analysisContext) {
        QueryWrapper<TblUnits> wrapper = new QueryWrapper<>();
        wrapper.eq("name",teacherUserData.getDepartment());
        Integer unitsId = unitsService.getOne(wrapper).getUnitsId();
        //添加进user表
        TblUser user = new TblUser();
//        //判断该角色是学生还是教师，0为学生，1为教师，默认为学生
//        int role = teacherUserData.getRoleId();
//        String idNumber = teacherUserData.getIdNumber();
//        user.setIdNumber(idNumber);
//        String telephone = teacherUserData.getTelephone();
//        user.setTelephone(telephone);
//        user.setPassword(teacherUserData.getPassword());
//        user.setRoleId(teacherUserData.getRoleId());
//        user.setDelFlag(teacherUserData.getDelFlag());
        BeanUtils.copyProperties(teacherUserData,user);
        user.setRoleId(2);
        user.setDelFlag(0);
        user.setPassword(passwordEncoder.encode(teacherUserData.getPassword()));
        user.setUnitsId(unitsId);
        tblUserService.save(user);
        //查询生成的userId
        int userId = user.getUserId();
        //添加进teacher表
        TblTeacher teacher = new TblTeacher();
//        teacher.setUserId(userId);
//        teacher.setTelephone(telephone);
//        teacher.setIdNumber(idNumber);
//        teacher.setName(teacherUserData.getName());
//        teacher.setSex(teacherUserData.getSex());
//        teacher.setAddress(teacherUserData.getAddress());
//        teacher.setSchool(teacherUserData.getSchool());
//        teacher.setCollege(teacherUserData.getCollege());
//        teacher.setTeacherNumber(teacherUserData.getTeacherNumber());
//        teacher.setDepartment(teacherUserData.getDepartment());
//        teacher.setAvatar(teacherUserData.getAvatar());
//        teacher.setDelFlag(teacherUserData.getDelFlag());
        BeanUtils.copyProperties(teacherUserData,teacher);
        teacher.setUserId(userId);
        teacher.setUnitsId(unitsId);
        tblTeacherService.save(teacher);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }
}
